package com.fengwk.cv4j.view.common;

import com.fengwk.cv4j.compute.Compute;
import com.fengwk.cv4j.factory.ViewFactory;
import com.fengwk.cv4j.view.View;
import com.fengwk.cv4j.view.ViewImpl;

/**
 * 取最小值
 * 
 * @author fengwk
 *
 * @param <T>
 */
public class MinView<T> extends ViewImpl<T> {

    private static final long serialVersionUID = -7153020580527538699L;

    public MinView(Compute<T> compute, ViewFactory<T> factory, @SuppressWarnings("unchecked") View<T>...vs) {
        super(compute, factory, vs);
        checkVsMin(1, vs);
    }

    @Override
    protected T compute0() {
        return c().min(ts());
    }

    @Override
    protected View<T> gradient0(int idx) {
    	View<T> count = f().of(0);
        for (View<T> tbuf: vs()) 
        	count = f().add(count, f().equals(tbuf, this));
        return f().mul(f().equals(v(idx), this), f().div(f().of(1), count));
    }
    
    @Override
    public String toString() {
        return "min";
    }

}
